#!/bin/bash
. ./cmd.sh
. ./path.sh

#training data
stage=2
dir=test_speaker-adaptation/data
data=data/test_hyp_eval
gmmdir=exp/tri4b
sidnn=exp/tri4b_pretrain-dbn_dnn

if [ $stage -le 0 ]; then
mkdir -p $dir
#for x in 440; do
for x in 441 442 443 444 445 446 447; do

  if [ -d $dir/$x ]; then
    rm -rf $dir/$x
  fi
  cp -r $data/$x $dir/$x
  mkdir -p $dir/$x

  mkdir $dir/$x/train
  mkdir $dir/$x/dev

  #just copy spk2gender, spk2utt and utt2spk files
  cp $dir/$x/spk2gender $dir/$x/dev/spk2gender
  cp $dir/$x/spk2gender $dir/$x/train/spk2gender

  cp $dir/$x/spk2utt $dir/$x/dev/spk2utt
  cp $dir/$x/spk2utt $dir/$x/train/spk2utt

  cp $dir/$x/utt2spk $dir/$x/dev/utt2spk
  cp $dir/$x/utt2spk $dir/$x/train/utt2spk

  #divide data for text and wav files
  sed -n '1,5p' $dir/$x/wav.scp > $dir/$x/dev/wav.scp
  sed -n '6,$p' $dir/$x/wav.scp > $dir/$x/train/wav.scp

  #divide the data for text files
  sed -n '1,5p' $dir/$x/text > $dir/$x/dev/text
  sed -n '6,$p' $dir/$x/text > $dir/$x/train/text
  #python divide_data.py $lines $dir/$x

  utils/fix_data_dir.sh $dir/$x/train/
  utils/fix_data_dir.sh $dir/$x/dev/
done
fi

dir1=test_speaker-adaptation/feats
if [ $stage -le 1 ]; then
mkdir -p $dir1
#for x in 440; do
for x in 441 442 443 444 445 446 447; do
 
  mkdir -p $dir1/$x/fbank

 steps/make_fbank.sh --cmd "$train_cmd" --nj 1 $data/$x exp/make_fbank1/$x $dir1/$x/fbank
 steps/compute_cmvn_stats.sh $data/$x exp/make_fbank1/$x $dir1/$x/fbank

 echo "processing  training data for speaker $x "  
 steps/make_fbank.sh --cmd "$train_cmd" --nj 1 $dir/$x/train exp/make_fbank/$x $dir1/$x/fbank/train
 steps/compute_cmvn_stats.sh $dir/$x/train exp/make_fbank/$x $dir1/$x/fbank/train
 steps/nnet/align.sh --nj 1 --cmd "$train_cmd" $dir/$x/train data/lang  exp/tri4b_pretrain-dbn_dnn  $dir1/$x/ali/train

 echo "processing  dev data for speaker $x "  
 steps/make_fbank.sh --cmd "$train_cmd" --nj 1 $dir/$x/dev exp/make_fbank/$x $dir1/$x/fbank/dev
 steps/compute_cmvn_stats.sh $dir/$x/dev exp/make_fbank/$x $dir1/$x/fbank/dev
 steps/nnet/align.sh --nj 1 --cmd "$train_cmd" $dir/$x/dev data/lang  exp/tri4b_pretrain-dbn_dnn  $dir1/$x/ali/dev

done
fi

mkdir test_speaker-adaptation/exp
if [ $stage -le 2 ]; then
#for x in 440; do
for x in 440 441 442 443 444 445 446 447; do
echo "Building the transformation for speaker $x"
#steps/nnet/train_transform.sh --delta_order 2 --feature-transform exp/tri4b_pretrain-dbn_dnn/final.feature_transform --learn-rate 0.000001 $dir/$x/train $dir/$x/dev data/lang $dir1/$x/ali/train $dir1/$x/ali/dev test_speaker-adaptation/exp/$x  $sidnn

#mkdir -p $sidnn/test_transforms
#cp test_speaker-adaptation/exp/$x/final.nnet $sidnn/test_transforms/$x.nnet

#steps/make_fbank.sh --cmd "$train_cmd" --nj 1 data/test_eval92/$x exp/make_fbank/$x $dir1/$x/fbank/train
#steps/compute_cmvn_stats.sh data/test_eval92/$x exp/make_fbank/$x $dir1/$x/fbank/train


steps/nnet/decode.sh --nj 1 --cmd "$decode_cmd" --acwt 0.10 --config conf/decode_dnn.config  $gmmdir/graph_bd_tgpr data/test_eval92/$x test_speaker-adaptation/exp/$x/decode_prior_$x
#steps/nnet/decode.sh --nj 1 --cmd "$decode_cmd" --acwt 0.10 --config conf/decode_dnn.config  $gmmdir/graph_bd_tgpr data/test_eval92/$x exp/tri4b_pretrain-dbn_dnn/decode_dev_$x

done
fi
